<?php
/**
 * @copyright meila.com
 * @author shaopu@
 * @name 检测昵称是否可用
 * @param 
 *         $nick = 作用
 * 
 * 
 */
include('../__global.php');

class aj_completeInfor extends ml_controller 
{
    private $pB;
    private $pE;

    function initParam()
    {

        $this->pB['astro'] = (int)$this->input('astro');

        $this->pE['birthday'] = $this->input('birthday');
        $this->pE['blood_type'] = (int)$this->input('blood_type');

    }
    /**
     * @develop_template
     * 检查参数合法性
     */
    function checkParam(){
        $this->check_referer();
        if(!$this->__visitor['online']) return $this->api_output(ML_RCODE_NOLOGIN);

        /*
        * 检查生日和星座
        */
        if(!empty($this->pE['birthday'])){
            if(!strtotime($this->pE['birthday'])){
                $this->api_output(ML_RCODE_PARAM);
            }
            if(empty($this->pB['astro'])) $this->api_output(ML_RCODE_PARAM);

            $datetime = strtotime($this->pE['birthday']);
            $datetimeA = explode('-', $this->pE['birthday']);
            $year = (int)$datetimeA[0];
            $astros = ml_factory::load_standard_conf('astro');


            $curAstors = $astros[$this->pB['astro']];
            if(empty($curAstors)) $this->api_output(ML_RCODE_PARAM);

            
            
            if($this->pB['astro'] == 1){
                $starttime = strtotime(($year-1).'-'.$curAstors[0]);
            }else{
                $starttime = strtotime($year.'-'.$curAstors[0]);
            }
            $endtime = strtotime($year.'-'.$curAstors[1]);


            if(!($datetime>=$starttime && $datetime<$endtime)) $this->api_output(ML_RCODE_PARAM);
        }else{
            if(!empty($this->pB['astro'])) $this->api_output(ML_RCODE_PARAM);
        }

        /**
        * 检查血型
        */
        if(!empty($this->pE['blood_type'])){
            $bloods = ml_factory::load_standard_conf('bloods');
            if(empty($bloods[$this->pE['blood_type']])) $this->api_output(ML_RCODE_PARAM);
        }
    }
    /**
     * @develop_template
     * 核心业务逻辑
     */
    function main()
    {
        if(empty($this->pE['birthday']) && empty($this->pE['blood_type'])){
            $this->api_output(ML_RCODE_SUCC);
        }
        $oBrief = new ml_model_dbUserBriefInfo();
        $oExt = new ml_model_dbUserExtInfo();
        
        $uid = $this->__visitor['uid'];

        $briefRes = $oBrief->updatePerson($uid, $this->pB);
        $extRes = $oExt->updatePerson($uid , $this->pE);

        if(false!=$briefRes && false!=$extRes)
        {
            $this->api_output(ML_RCODE_SUCC);
        }
        else 
        {
            $this->api_output(ML_RCODE_BUSY , '' , ML_RMESSAGE_BUSY);
        }
    }
}

new aj_completeInfor();
